package dao.impl;

import dao.BaseDao;
import dao.BookManageDao;
import entity.BookManage;
import utils.DataBaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 1.继承BaseDao
 * 2.实现BookManageDao接口
 * 3.重写接口中的抽象方法
 */
public class BookManageDaoImpl extends BaseDao implements BookManageDao {
    public BookManageDaoImpl(Connection conn) {
        super(conn);
    }

    @Override
    public List<BookManage> findAll() throws SQLException {
        List<BookManage> books=new ArrayList<>();
        String sql="select b_id,b_name,b_author,b_time,b_type from bookmanage order by b_time desc;";
        PreparedStatement stmt = this.conn.prepareStatement(sql);
        ResultSet rs = super.query(sql, stmt);
        while (rs.next()) {
            BookManage book=new BookManage(
                    rs.getLong("b_id"),
                    rs.getString("b_name"),
                    rs.getString("b_author"),
                    rs.getDate("b_time"),
                    rs.getInt("b_type")
            );
            books.add(book);
        }
        //关闭资源
        DataBaseUtil.closeAll(conn,stmt,rs);
        return books;
    }

    @Override
    public int insert(BookManage book) throws SQLException {
        return 0;
    }

    @Override
    public int deleteById(String id) throws SQLException {
        return 0;
    }
}
